/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFHistoricos.java
 *
 * Created on 05/12/2012, 09:37:40
 */
package contatospro;

import java.awt.Component;
import java.awt.EventQueue;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author Junnyor S2 Rafa
 */
public class JFHistoricos extends JPanel {
    private int codPessoa;
    
    public JFHistoricos() {
        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("contatosproPU").createEntityManager();
        query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT h FROM HistoricoContatos h");
        list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
        pessoasQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT p FROM Pessoas p");
        pessoasList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : pessoasQuery.getResultList();
        contatosQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Contatos c");
        contatosList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : contatosQuery.getResultList();
        historicoContatos1 = new contatospro.HistoricoContatos();
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        pessoajComboBox = new javax.swing.JComboBox();
        historicoLabel = new javax.swing.JLabel();
        codMembroLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        historicoConversajTextArea = new javax.swing.JTextArea();
        dataUltimoContatoLabel = new javax.swing.JLabel();
        horaUltimoContatoLabel = new javax.swing.JLabel();
        situacoesjButton1 = new javax.swing.JButton();
        dataUltimoContatojFormattedTextField = new javax.swing.JFormattedTextField();
        horaUltimoContatojFormattedTextField = new javax.swing.JFormattedTextField();
        contatosField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        newButton1 = new javax.swing.JButton();
        saveButton1 = new javax.swing.JButton();
        refreshButton1 = new javax.swing.JButton();
        deleteButton1 = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codContato}"), historicoContatos1, org.jdesktop.beansbinding.BeanProperty.create("codContato"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codHistorico}"), historicoContatos1, org.jdesktop.beansbinding.BeanProperty.create("codHistorico"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codPessoa}"), historicoContatos1, org.jdesktop.beansbinding.BeanProperty.create("codPessoa"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.dataUltimoContato}"), historicoContatos1, org.jdesktop.beansbinding.BeanProperty.create("dataUltimoContato"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.historicoConversa}"), historicoContatos1, org.jdesktop.beansbinding.BeanProperty.create("historicoConversa"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.horaUltimoContato}"), historicoContatos1, org.jdesktop.beansbinding.BeanProperty.create("horaUltimoContato"));
        bindingGroup.addBinding(binding);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codHistorico}"));
        columnBinding.setColumnName("Cod Historico");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codContato.codContato}"));
        columnBinding.setColumnName("Cod Contato");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codPessoa.pessoa}"));
        columnBinding.setColumnName("Pessoa");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterTable.addMouseListener(formListener);
        masterScrollPane.setViewportView(masterTable);

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setName("jPanel2"); // NOI18N

        pessoajComboBox.setName("pessoajComboBox"); // NOI18N
        pessoajComboBox.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof Pessoas) {
                    Pessoas mec = (Pessoas)value;
                    setText(mec.getPessoa());
                }
                return this;
            }
        });

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, pessoasList, pessoajComboBox);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codPessoa}"), pessoajComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        historicoLabel.setText("Histórico de Conversa:");
        historicoLabel.setName("historicoLabel"); // NOI18N

        codMembroLabel.setText("Pessoa");
        codMembroLabel.setName("codMembroLabel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        historicoConversajTextArea.setColumns(20);
        historicoConversajTextArea.setRows(5);
        historicoConversajTextArea.setName("historicoConversajTextArea"); // NOI18N
        jScrollPane1.setViewportView(historicoConversajTextArea);

        dataUltimoContatoLabel.setText("Data Ultimo Contato:");

        horaUltimoContatoLabel.setText("Hora Ultimo Contato:");

        situacoesjButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/lupa1.png"))); // NOI18N
        situacoesjButton1.setFocusable(false);
        situacoesjButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        situacoesjButton1.setName("situacoesjButton1"); // NOI18N
        situacoesjButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        situacoesjButton1.addActionListener(formListener);

        dataUltimoContatojFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM))));
        dataUltimoContatojFormattedTextField.setName("dataUltimoContatojFormattedTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, historicoContatos1, org.jdesktop.beansbinding.ELProperty.create("${dataUltimoContato}"), dataUltimoContatojFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("value"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), dataUltimoContatojFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        horaUltimoContatojFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT))));
        horaUltimoContatojFormattedTextField.setName("horaUltimoContatojFormattedTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, historicoContatos1, org.jdesktop.beansbinding.ELProperty.create("${horaUltimoContato}"), horaUltimoContatojFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("value"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), horaUltimoContatojFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        contatosField.setName("contatosField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, historicoContatos1, org.jdesktop.beansbinding.ELProperty.create("${codContato}"), contatosField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), contatosField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jLabel1.setText("Cod Contato");
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(codMembroLabel)
                    .addComponent(historicoLabel)
                    .addComponent(jLabel1))
                .addGap(14, 14, 14)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(pessoajComboBox, 0, 530, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(situacoesjButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(contatosField, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(dataUltimoContatoLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(dataUltimoContatojFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(horaUltimoContatoLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(horaUltimoContatojFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(pessoajComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(codMembroLabel))
                    .addComponent(situacoesjButton1))
                .addGap(6, 6, 6)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(horaUltimoContatojFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(horaUltimoContatoLabel)
                    .addComponent(dataUltimoContatojFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(contatosField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dataUltimoContatoLabel)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(historicoLabel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jToolBar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar1.setFloatable(false);
        jToolBar1.setName("jToolBar1"); // NOI18N

        newButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/Novo6.png"))); // NOI18N
        newButton1.setText("Novo");
        newButton1.setToolTipText("Novo Cadastro");
        newButton1.setFocusable(false);
        newButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newButton1.setMaximumSize(new java.awt.Dimension(50, 60));
        newButton1.setName("newButton1"); // NOI18N
        newButton1.setPreferredSize(new java.awt.Dimension(50, 60));
        newButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        newButton1.addActionListener(formListener);
        jToolBar1.add(newButton1);

        saveButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/Salvar1.png"))); // NOI18N
        saveButton1.setText("Salvar");
        saveButton1.setToolTipText("Salvar Pessoa");
        saveButton1.setFocusable(false);
        saveButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveButton1.setMaximumSize(new java.awt.Dimension(50, 60));
        saveButton1.setName("saveButton1"); // NOI18N
        saveButton1.setPreferredSize(new java.awt.Dimension(50, 60));
        saveButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        saveButton1.addActionListener(formListener);
        jToolBar1.add(saveButton1);

        refreshButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/Atualizar1.png"))); // NOI18N
        refreshButton1.setText("Atualizar");
        refreshButton1.setToolTipText("Atualizar Tela");
        refreshButton1.setFocusable(false);
        refreshButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        refreshButton1.setMaximumSize(new java.awt.Dimension(65, 60));
        refreshButton1.setName("refreshButton1"); // NOI18N
        refreshButton1.setPreferredSize(new java.awt.Dimension(65, 60));
        refreshButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        refreshButton1.addActionListener(formListener);
        jToolBar1.add(refreshButton1);

        deleteButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/Excluir3.png"))); // NOI18N
        deleteButton1.setText("Excluir");
        deleteButton1.setFocusable(false);
        deleteButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deleteButton1.setMaximumSize(new java.awt.Dimension(50, 60));
        deleteButton1.setName("deleteButton1"); // NOI18N
        deleteButton1.setPreferredSize(new java.awt.Dimension(50, 60));
        deleteButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteButton1, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        deleteButton1.addActionListener(formListener);
        jToolBar1.add(deleteButton1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 729, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 729, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(masterScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener, java.awt.event.MouseListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == newButton1) {
                JFHistoricos.this.newButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == saveButton1) {
                JFHistoricos.this.saveButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == refreshButton1) {
                JFHistoricos.this.refreshButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == deleteButton1) {
                JFHistoricos.this.deleteButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == situacoesjButton1) {
                JFHistoricos.this.situacoesjButton1ActionPerformed(evt);
            }
        }

        public void mouseClicked(java.awt.event.MouseEvent evt) {
            if (evt.getSource() == masterTable) {
                JFHistoricos.this.masterTableMouseClicked(evt);
            }
        }

        public void mouseEntered(java.awt.event.MouseEvent evt) {
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
        }

        public void mousePressed(java.awt.event.MouseEvent evt) {
        }

        public void mouseReleased(java.awt.event.MouseEvent evt) {
        }
    }// </editor-fold>//GEN-END:initComponents

    
            
private void deleteButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButton1ActionPerformed
        int[] selected = masterTable.getSelectedRows();
        List<contatospro.HistoricoContatos> toRemove = new ArrayList<contatospro.HistoricoContatos>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
            contatospro.HistoricoContatos h = list.get(masterTable.convertRowIndexToModel(selected[idx]));
            String nome = ("Deseja Realmente Excluir Essa Pessoa ?");  
        int opcao_escolhida = JOptionPane.showConfirmDialog(null,nome,"Excluir Pessoa ",JOptionPane.YES_NO_OPTION);  
        if (opcao_escolhida==JOptionPane.YES_OPTION){  
        toRemove.add(h);
        entityManager.remove(h);
        }
        else  return;
        JOptionPane.showMessageDialog(null, "Pessoa Excluída com Sucesso!!");
        }
        list.removeAll(toRemove);   
        
}//GEN-LAST:event_deleteButton1ActionPerformed

private void refreshButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButton1ActionPerformed
        entityManager.getTransaction().rollback();
        entityManager.getTransaction().begin();
        java.util.Collection data = query.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        list.clear();
        list.addAll(data);
}//GEN-LAST:event_refreshButton1ActionPerformed

private void saveButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButton1ActionPerformed
        try {
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
            JOptionPane.showMessageDialog(null, "Histórico Cadastrado com Sucesso!!");

        } catch (RollbackException rex) {
            rex.printStackTrace();
            entityManager.getTransaction().begin();
            List<contatospro.HistoricoContatos> merged = new ArrayList<contatospro.HistoricoContatos>(list.size());
            for (contatospro.HistoricoContatos h : list) {
                merged.add(entityManager.merge(h));
            }
            list.clear();
            list.addAll(merged);
        }
}//GEN-LAST:event_saveButton1ActionPerformed

private void newButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButton1ActionPerformed
        contatospro.HistoricoContatos h = new contatospro.HistoricoContatos();
        entityManager.persist(h);
        list.add(h);
        int row = list.size() - 1;
        masterTable.setRowSelectionInterval(row, row);
        masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
        //Adicionar data do dia ao campo JPA
        Calendar calendario = Calendar.getInstance();
        historicoContatos1.setDataUltimoContato(calendario.getTime());  
        historicoContatos1.setHoraUltimoContato(calendario.getTime());
}//GEN-LAST:event_newButton1ActionPerformed

private void situacoesjButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_situacoesjButton1ActionPerformed
// TODO add your handling code here:
       JFrame frame = new JFrame("Pessoas");
         frame.setContentPane(new JFPessoas());
         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         frame.pack();
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);
}//GEN-LAST:event_situacoesjButton1ActionPerformed

private void masterTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_masterTableMouseClicked
// TODO add your handling code here:
     
}//GEN-LAST:event_masterTableMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel codMembroLabel;
    private javax.swing.JTextField contatosField;
    private java.util.List<contatospro.Contatos> contatosList;
    private javax.persistence.Query contatosQuery;
    private javax.swing.JLabel dataUltimoContatoLabel;
    private javax.swing.JFormattedTextField dataUltimoContatojFormattedTextField;
    private javax.swing.JButton deleteButton1;
    private javax.persistence.EntityManager entityManager;
    private contatospro.HistoricoContatos historicoContatos1;
    private javax.swing.JTextArea historicoConversajTextArea;
    private javax.swing.JLabel historicoLabel;
    private javax.swing.JLabel horaUltimoContatoLabel;
    private javax.swing.JFormattedTextField horaUltimoContatojFormattedTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private java.util.List<contatospro.HistoricoContatos> list;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JButton newButton1;
    private javax.swing.JComboBox pessoajComboBox;
    private java.util.List<contatospro.Pessoas> pessoasList;
    private javax.persistence.Query pessoasQuery;
    private javax.persistence.Query query;
    private javax.swing.JButton refreshButton1;
    private javax.swing.JButton saveButton1;
    private javax.swing.JButton situacoesjButton1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    public static void main(String[] args) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFHistoricos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFHistoricos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFHistoricos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFHistoricos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new JFHistoricos());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
